package com.ssi.myshop.ui.adapter;

import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ssi.myshop.R;
import com.ssi.myshop.bean.FloorListBean;
import com.ssi.myshop.ui.custom.AutoNextLineLinearLayout;
import com.ssi.myshop.utils.SizeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LinearItemContentAdapter extends RecyclerView.Adapter<LinearItemContentAdapter.InnerHolder> {
    private static final String TAG = "LinearItemContentAdapte";
    List<FloorListBean.MessageBean> mData = new ArrayList<>();

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_linear_content, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        FloorListBean.MessageBean messageBean = mData.get(position);
        holder.setData(messageBean);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_top_img)
        public ImageView topImg;

        @BindView(R.id.item_linear_box)
        public AutoNextLineLinearLayout linearBox;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


        public void setData(FloorListBean.MessageBean messageBean) {
            Glide.with(itemView.getContext()).load(messageBean.getFloor_title().getImage_src()).into(topImg);
            WindowManager manager = (WindowManager) itemView.getContext()
                    .getSystemService(Context.WINDOW_SERVICE);
            Display defaultDisplay = manager.getDefaultDisplay();
            int width = defaultDisplay.getWidth();
            List<ImageView> imageViewList = new ArrayList<>();
            for (int i = 0; i < messageBean.getProduct_list().size(); i++) {
                Context context = itemView.getContext();
                ImageView imageView = new ImageView(context);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width / 3, SizeUtils.dip2px(context, 200));
                ViewGroup.LayoutParams layoutParams2 = new ViewGroup.LayoutParams(width / 3, SizeUtils.dip2px(context, 100));
                if (i == 0) {
                    imageView.setLayoutParams(layoutParams2);
                } else {
                    imageView.setLayoutParams(layoutParams2);
                }
                Glide.with(context).load(messageBean.getProduct_list().get(i).getImage_src()).into(imageView);
//                imageView.setScaleType(ImageView.ScaleType.CENTER);
//                linearBox.addView(imageView);
                imageViewList.add(imageView);
            }

            linearBox.setImageList(imageViewList);
        }
    }

    public void setmData(List<FloorListBean.MessageBean> messageBeanList) {
        mData.clear();
        mData.addAll(messageBeanList);
        notifyDataSetChanged();
    }
}
